<template>
  <div class="goodslist-container">
    <!-- <router-link :to="'/home/goodsinfo/'+ item.id" tag="div" class="itemBox" v-for="item in goodslist" :key="item.id">
      <img :src="item.img_url" alt>
      <p class="title">{{ item.title}}</p>
      <div class="info">
        <p class="price">
          <span class="now">￥{{ item.sell_price }}</span>
          <span class="old">￥{{ item.market_price }}</span>
        </p>
        <p class="subtitle">
          <span class="remai">热卖中</span>
          <span class="leave">剩{{ item.stock_quantity }}件</span>
        </p>
      </div>
    </router-link> -->

    <div class="itemBox" v-for="item in goodslist" @click="getDetail(item.id)" :key="item.id">
      <img :src="item.img_url" alt>
      <p class="title">{{ item.title}}</p>
      <div class="info">
        <p class="price">
          <span class="now">￥{{ item.sell_price }}</span>
          <span class="old">￥{{ item.market_price }}</span>
        </p>
        <p class="subtitle">
          <span class="remai">热卖中</span>
          <span class="leave">剩{{ item.stock_quantity }}件</span>
        </p>
      </div>
    </div>
  <mt-button type="danger" size="large" @click = "getMore">加载更多</mt-button>
  </div>
</template>

<script>
export default {
  data(){
    return {
      pageindex : 1,
      goodslist:[]//存放商品列表的数组
    }
  },
  created(){
    this.getGoodsList()
  },
  methods:{
    getGoodsList(){
      this.$http.get('api/getgoods?pageindex=' + this.pageindex)
      .then(res=>{
        // console.log(res)
        if(res.body.status === 0){
          this.goodslist = this.goodslist.concat(res.body.message);
        }
      })
    },
    getMore(){
      this.pageindex++;
      this.getGoodsList()
    },
    getDetail(id){
      // 使用 JS 形式的形式进行路由导航

      // 注意： 一定要区分 this.$route 和 this.$router 这两个对象
      // 其中 this.$route 是路由【参数对象】 ， 所有路由中的参数，params query 都属于它
      // 其中： this.$router 是一个路由【导航对象】 用它可以方便的使用JS 代码，实现路由的前进、后退、跳转到新的 URL 地址
      // 最基本的用法
      // this.$router.push('/home/goodsinfo/' + id );

      // 对象
      // this.$router.push({path:`/home/goodsinfo/${id}` } );

      // 传递命名的路由
      this.$router.push({ name:"goodsinfo" , params:{id} });



    }
  }

};
</script>


<style lang="scss" scoped>
.goodslist-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 8px;
  .itemBox {
    display: flex;
    flex-direction: column;
      justify-content: space-between;
    box-sizing: border-box;
    width: 49%;
    border: 1px solid #ccc;
    margin-bottom: 6px;
    box-shadow: 0 0 3px rgba(0, 0, 0, 0.1);
      min-height: 200px;
      position: relative;
    img {
      width: 100%;
    }
    .title {
      color: #000;
      font-weight: 700;
      padding:0 5px;
    }

    .info {
      
      flex-direction: column;
      justify-content: space-between;
      bottom: 0;
      width:100%;
      p{
        margin:0;
        height:30px;
        line-height: 30px;
      }
      background-color: #eee;
        padding:0 5px;
      .price {
        .now {
          font-size: 16px;
          color: red;
          font-weight: 700;
        }
        .old {
          font-size: 12px;
          color: #bbb;
          margin-left: 10px;
          text-decoration: line-through;
        }
      }
      .subtitle{
        display: flex;
        justify-content: space-between;
        .remai{

        }
      }
    }
  }
}
</style>
